package com.mths.client.page;

import java.util.Date;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.CalendarUtil;
import com.mths.client.component.Button;
import com.mths.client.component.DateField;
import com.mths.client.component.GridData;
import com.mths.client.controller.LogHistoryController;
import com.mths.client.controller.LogHistoryControllerAsync;
import com.mths.client.dto.LogHistoryDTO;
import com.mths.client.dto.LogHistoryData;
import com.mths.client.util.AsyncCall;

public class LogHistory extends AbstractPage implements IPage {

	private static LogHistoryUiBinder		uiBinder	= GWT.create(LogHistoryUiBinder.class);

	private final LogHistoryControllerAsync	controller	= GWT.create(LogHistoryController.class);

	interface LogHistoryUiBinder extends UiBinder<Widget, LogHistory> {
	}

	private LogHistoryDTO		dto	= new LogHistoryDTO();

	@UiField
	DateField					startDate, finishDate;

	@UiField
	Button						searchBtn;

	@UiField
	GridData<LogHistoryData>	logGridData;

	public LogHistory() {
		initWidget(uiBinder.createAndBindUi(this));
		initEventHandler();
		initGridData();
		initDateField();
	}

	private void initDateField() {
		Date startDateInit = new Date();
		CalendarUtil.addMonthsToDate(startDateInit, -1);
		startDate.setFieldValue(startDateInit);
		finishDate.setFieldValue(new Date());
	}

	private void initEventHandler() {
		searchBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				searchLogHistory();

			}

		});
	}

	private void searchLogHistory() {
		dto.setStartDate(startDate.getFieldValue());
		dto.setFinishDate(finishDate.getFieldValue());
		controller.searchLogHistoryByDate(dto, new AsyncCall<List<LogHistoryData>>() {
			@Override
			public void successCall(List<LogHistoryData> result) {
				logGridData.clear();
				logGridData.putDataList(result);
			}
		});
	}

	private void initGridData() {
		TextColumn<LogHistoryData> actionLogType = new TextColumn<LogHistoryData>() {

			@Override
			public String getValue(LogHistoryData object) {
				return object.getActionLogType();
			}
		};
		TextColumn<LogHistoryData> userName = new TextColumn<LogHistoryData>() {

			@Override
			public String getValue(LogHistoryData object) {
				return object.getUser();
			}
		};
		TextColumn<LogHistoryData> effectedUserName = new TextColumn<LogHistoryData>() {

			@Override
			public String getValue(LogHistoryData object) {
				return object.getEffectedUser();
			}
		};
		TextColumn<LogHistoryData> companyName = new TextColumn<LogHistoryData>() {

			@Override
			public String getValue(LogHistoryData object) {
				return object.getCompany();
			}
		};
		TextColumn<LogHistoryData> documentTypeName = new TextColumn<LogHistoryData>() {

			@Override
			public String getValue(LogHistoryData object) {
				return object.getDocumentType();
			}
		};
		TextColumn<LogHistoryData> directorName = new TextColumn<LogHistoryData>() {

			@Override
			public String getValue(LogHistoryData object) {
				return object.getDirector();
			}
		};
		TextColumn<LogHistoryData> inspectorName = new TextColumn<LogHistoryData>() {

			@Override
			public String getValue(LogHistoryData object) {
				return object.getInspector();
			}
		};

		logGridData.addColumn(actionLogType, messages.LogHistory_actionLogType());
		logGridData.addColumn(userName, messages.LogHistory_userName());
		logGridData.addColumn(effectedUserName, messages.LogHistory_effectedUserName());
		logGridData.addColumn(companyName, messages.LogHistory_companyName());
		logGridData.addColumn(documentTypeName, messages.LogHistory_documentTypeName());
		logGridData.addColumn(directorName, messages.LogHistory_directorName());
		logGridData.addColumn(inspectorName, messages.LogHistory_inspectorName());
	}

	@Override
	public String pageName() {
		return messages.LogHistory_pageName();
	}
}